package com.ihighest.app.infox.activity;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.core.Application;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.SelecMyColumnListAdapter;
import com.ihighest.app.infox.fragment.Nj_Fragment_new.ReflashBroadcast;
import com.ihighest.app.infox.myview.ActivityTitle;

/**
 * 订阅列表界面
 * 
 * @author lkx Created at 2015-10-7 下午5:15:21
 * @version 1.0
 */
public class SubscriptionListActivity extends BaseActivity {
	private ActivityTitle title;
	private PullToRefreshListView SubscriptionList;
	private SelecMyColumnListAdapter adapter;
	private String channelId;
	int page = 1;
	private JSONArray array;
	private TextView emptyview;
	private JSONArray moreArray;
	private ReflashBroadcast reflashBroadcast;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Application.getInstance().addActivity(this);
		setContentView(R.layout.infox_activity_subscriptionlist);
		title = (ActivityTitle) findViewById(R.id.infox_SubscriptionListTitle);
		title.setTitle("订阅更多");
		title.setViewVersible(title.titleBack);
		title.setViewGone(title.titleMsg, title.titleRefresh);
		channelId = getIntent().getStringExtra("channelId");
		init();
	}

	/**
	 * 初始化
	 * 
	 * 
	 * @author lkx Created at 2015-10-7 下午5:21:45
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void init() {

		reflashBroadcast = new ReflashBroadcast();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Const.REFLASH);
		this.registerReceiver(reflashBroadcast, filter);
		SubscriptionList = (PullToRefreshListView) findViewById(R.id.SubscriptionList);

		adapter = new SelecMyColumnListAdapter(SubscriptionListActivity.this, array);
		SubscriptionList.setAdapter(adapter);
		SubscriptionList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				page = 1;
				getData();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				page++;
				getData();
			}

		});
		SubscriptionList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// item点击事件
				try {
					if (array.getJSONObject(position - 1).getString("type").equals("Columnist")) {
						// 专栏数据
						Intent intent = new Intent(SubscriptionListActivity.this, ColumnNameActivity.class);
						intent.putExtra("cid", array.getJSONObject(position - 1).getString("id"));
						// intent.putExtra("name", array.getJSONObject(position
						// - 1).getString("title"));
						// intent.putExtra("peoplenumber",
						// array.getJSONObject(position - 1).getInt("count"));
						// intent.putExtra("imageurl",
						// array.getJSONObject(position -
						// 1).getString("image"));
						// intent.putExtra("subscription",
						// array.getJSONObject(position -
						// 1).getString("isSubscribes"));
						startActivity(intent);
					} else {
						// 企业号数据
						Intent intent = new Intent(SubscriptionListActivity.this, CompanyPagesActivity.class);
						intent.putExtra("cid", array.getJSONObject(position - 1).getString("id"));
						// intent.putExtra("name", array.getJSONObject(position
						// - 1).getString("title"));
						// intent.putExtra("peoplenumber",
						// array.getJSONObject(position - 1).getInt("count"));
						// intent.putExtra("imageurl",
						// array.getJSONObject(position -
						// 1).getString("image"));
						// intent.putExtra("subscription",
						// array.getJSONObject(position -
						// 1).getString("isSubscribes"));
						// intent.putExtra("describe",
						// array.getJSONObject(position - 1).getString("memo"));
						// intent.putExtra("phonenumber",
						// array.getJSONObject(position -
						// 1).getString("phone"));
						// intent.putExtra("service",
						// array.getJSONObject(position -
						// 1).getString("service"));
						startActivity(intent);
					}
					// ToastKit.showShort(SubscriptionListActivity.this,
					// Const.UN_FINISH_ALERT);
				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
		});
		getData();
	}

	/**
	 * 获取数据
	 * 
	 * 
	 * @author lkx Created at 2015-10-21 下午3:22:41
	 */
	private void getData() {
		Api.get("channel/subscribes", ApiParam.create().addParam("cid", channelId).addParam("page", page).addParam("type", "all"),
				new ApiCallback(SubscriptionListActivity.this, true) {

					@Override
					public void handleSuccess(ApiResult result) {
						if (result.getDatas() == null || result.getDatas().length() == 0) {
							if (page == 1) {
								emptyview.setText("暂无数据");
							} else {
								page = page > 1 ? --page : 1;
							}

						} else {
							if (page == 1) {
								array = result.getDatas();
								adapter.setData(array);
							} else {
								moreArray = result.getDatas();
								try {
									array = AppKit.joinJSONArray(array, moreArray);
								} catch (JSONException e) {
									e.printStackTrace();
								}
								adapter.appendData(moreArray);
							}
						}
						SubscriptionList.onRefreshComplete();
					}

					@Override
					public void handleFailure(Exception e, String msg) {
						Log.e("subscription", msg, e);
						SubscriptionList.onRefreshComplete();
					}
				}, SubscriptionListActivity.this);

	}

	public class ReflashBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// 得到数据变化的广播
			if (intent.getAction().equals(Const.REFLASH)) {
				page = 1;
				getData();

			}

		}

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		page = 1;
		getData();
	}

}
